package com.qf.agro.facilities;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.common.Constant;
import com.qf.agro.service.facilities.FacilitiesService;
import com.qf.agro.service.facilities.facilitiesConfigService;
import com.qf.agro.vo.OptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import com.qf.agro.common.ResultInfo;
import java.util.List;
@RestController
@RequestMapping("/facilities/facilities")
public class FacilitiesController {

    @Autowired
    private FacilitiesService facilitiesService;

    /**
     * 分页查询农业设施分类
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @Cacheable(key ="'page'" ,value = Constant.SMART_CACHE)
    public ResultInfo findPage(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                               String name){
        //1.调用业务层查询
        Page<Facilities> page = facilitiesService.findPage(pageNum, pageSize, name);
        //2.返回
        return ResultInfo.success(page);
    }


    /**
     * 新增分类
     * @param facilities
     * @return
     */
    @PostMapping
    @CacheEvict(key="'save'",value = Constant.SMART_CACHE)
    @CachePut(key = "#facilities.id",value = Constant.SMART_PREFIX_CACHE)
    public ResultInfo save(@RequestBody Facilities facilities){
        //1.调用业务层新增
        facilitiesService.save(facilities);
        //2.返回
        return ResultInfo.success(null);
    }

    /**
     * 修改分类
     * @param facilities
     * @return
     */
    @PutMapping
    @CacheEvict(key="'update'",value = Constant.SMART_CACHE)
    @CachePut(key = "#facilities.id",value = Constant.SMART_PREFIX_CACHE)
    public ResultInfo update(@RequestBody Facilities facilities){
        //1.调用业务层
        facilitiesService.update(facilities);
        //2.返回
        return ResultInfo.success(null);
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    @CacheEvict(key="'delete'",value = Constant.SMART_CACHE)
    @CachePut(key = "#id",value = Constant.SMART_PREFIX_CACHE)
    public ResultInfo delete(Long id){
        //1.调用业务层删除
        facilitiesService.delete(id);
        //2.返回
        return ResultInfo.success(null);
    }

    /**
     *
     * @return
     */
    @GetMapping("/selectOptions")
    @Cacheable(key ="'option'" ,value =Constant.SMART_CACHE)
    public ResultInfo selectFacilitiesOptions(){
        //1调用业务层查询
        List<OptionVo> optionVoList=facilitiesService.selectOptions();
        //返回
        return  ResultInfo.success(optionVoList);
    }
}
